import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../repository/datas/knowledge_list_data.dart';
import 'knowledge_detail_vm.dart';
import 'knowledge_tab_child_page.dart';

class KnowledgeDetailTabPage extends StatefulWidget {
  const KnowledgeDetailTabPage({super.key, this.tabList});

  final List<KnowledgeChildren>? tabList;

  @override
  State createState() {
    return _KnowledgeDetailTabPageState();
  }
}

class _KnowledgeDetailTabPageState extends State<KnowledgeDetailTabPage>
    with SingleTickerProviderStateMixin {
  KnowledgeDetailViewModel viewModel = KnowledgeDetailViewModel();

  //懒初始化
  late TabController tabController;

  @override
  void initState() {
    super.initState();
    tabController = TabController(
      length: widget.tabList?.length ?? 0,
      vsync: this,
    );
    viewModel.initTabs(widget.tabList);
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) {
        return viewModel;
      },
      child: Scaffold(
        appBar: AppBar(
          title: TabBar(
            tabs: viewModel.tabs,
            controller: tabController,
            labelColor: Colors.green,
            indicatorColor: Colors.green,
            isScrollable: true,
          ),
        ),
        body: SafeArea(child: TabBarView(controller: tabController,children: children(),)),
      ),
    );
  }

  List<Widget> children(){
    return widget.tabList?.map((e){
      return KnowledgeTabChildPage(cid: "${e.id ?? "" }");
    }).toList()?? [];
  }

}
